

#include "fileOperations.h"
#include "fileHandling.h"
#include "hashtable.h"
#include "bst.h"
/*/////////////////////////////////////////////////////////////////////////////
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~//Function Definitions//~~~~~~~~~~~~~~~~~~~~~~~~~~
////////////////////////////////////////////////////////////////////////////*/


// Function to add file, available on HD, to the VFS //

int addFile(char* destDirPath, char* fileName, char* dataFilePath) {

	long offset = 0;
	long offsetClear=0;
	int size = 0;
	char *address = (char*) malloc(strlen(destDirPath) + 1 + strlen(fileName) + 5);
	directory* temp;

	//  printf("%s:%s:%s:",destDirPath,fileName,dataFilePath);

	if (mountStatus == unMounted) {
		printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_07);
		return addfile_FAILURE;
	}
	
	if(head == NULL){
	printf("addfile_FAILURE\n");
		return addfile_FAILURE;
	}
	
	//INVALID_CHARACTER_IN_FILENAME
	if (invalidChar(fileName, 0) == 1) {
		printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_02);
		return addfile_FAILURE;
	}

	if (invalidChar(destDirPath, 1) == 1) {
		printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_05);
		return addfile_FAILURE;
	}

	//FILE_SYSTEM_FULL
	if (noOfFiles == noOfFileDescriptors) {
		printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_04);
		return addfile_FAILURE;
	}

	strcpy(address, destDirPath);
	if (strcmp(destDirPath, "/") != 0)
		strcat(address, "/");
	strcat(address, fileName);
	// printf("Adding :%s: :%d:", fileName, strlen(fileName));
	temp = creatNode(destDirPath, fileName, 'f', 1);
	//printf("Added%s %d",temp->name,strlen(temp->name));
	if (temp != NULL) {
		//   printf("Tree Updated\n");

		//returned pointer temp is not the newly added node
		if (strcmp(temp->name, fileName) != 0) {
			printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_05);
			return addfile_FAILURE;
		}

		offsetClear= offset = updateFreeBlockList();
		if (offset == -2) {
			deleteNode(temp);
			printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_05);
			return addfile_FAILURE;
		}

		//  printf("Free Block List Updated\n");
		size = updateBuffer(offset, dataFilePath);
		//    printf("Buffer Updated\n");

		if (size == -2) {
			printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_06);
			setBlockFree(offset, 1);
			deleteNode(temp);
			return addfile_FAILURE;
		}
		if(size == -3){
			printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_05);
			setBlockFree(offset, 1);
			deleteNode(temp);
			return addfile_FAILURE;
		}

		if(size==-5){
		printf("addfile_FAILURE\n");
		deleteNode(temp);
		setBlockFree(offsetClear, 1);
		return addfile_FAILURE;
		}
		
		offset = updateFileDescriptor(address, fileName, 'f', size, bufferOffset + offset * sizeof(buffer));
		//   printf("File Descriptor Updated\n");
		// Code to update naryTree
		
		if(offset==-5){
		printf("addfile_FAILURE\n");
		deleteNode(temp);
		setBlockFree(offsetClear, 1);
		return addfile_FAILURE;
		}
		
		temp->fileDescOffset = offset;
		//     printf("Offset : %d\n", offset);
		insertFile(temp->name,offset);
		add_elt(findPath(temp));
		printf("addfile_SUCCESS\n");
		return addfile_SUCCESS;
	} else {
		printf("addfile_FAILURE %s\n", ERR_VFS_ADDFILE_03);
		return addfile_FAILURE;
	}
}

// List all the files inside the given directory, and output the results to file on HD //

int listFile(char* sourceFileAddress, char* outputFileAddress) {
	//  printf("Listing contents of file");
	FILE *rdPtr;
	FILE *wrtPtr;
	directory *temp;
	char ch = 'p';
	int size = 0;
	char* path = (char*) malloc(strlen(sourceFileAddress) + 5);
	char name[fileNameLength];

	//printf("%s:%s:",sourceFileAddress,outputFileAddress);

	if (mountStatus == unMounted) {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_04);
		return listfile_FAILURE;
	}
	
	if(head==NULL){
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_01);
		return listfile_FAILURE;
	}
	
	if (invalidChar(sourceFileAddress, 1) == 1) {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_01);
		return listfile_FAILURE;
	}


	strcpy(path, sourceFileAddress);
	strcpy(name, getFileDirName(path));

	FileDescriptor fDes;
	iNode* inode = (iNode*) malloc(sizeof(iNode));
	inode->dirName = (char*) malloc(fileNameLength);

	if(traceIPath(sourceFileAddress, inode)==NULL){
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_04);
		return listfile_FAILURE;
	}
	temp = inode->parent;

	//temp=temp->child;
	// printf("%s:",temp->name);

	//SOURCE_FILE_PATH_NOT_FOUND
	if (strcmp(name, temp->name) != 0) {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_01);
		return listfile_FAILURE;
	}

	if (temp->type!='f') {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_03);
		return listfile_FAILURE;
	} 
	
	char *strtok(char *str1, const char *str2);
	char delims[] = ".";
	char *extension = strtok(name, delims);
	if(extension!=NULL)
	extension = strtok(NULL, delims);
	else{
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_03);
		return listfile_FAILURE; // file is not a text file
	}
	
	//NOT_A_TEXT_FILE
	if(extension==NULL ){
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_03);
		return listfile_FAILURE; 
	}
	if ( strcmp(extension, "txt") != 0) {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_03);
		return listfile_FAILURE; // file is not a text file
	}
		

	//CANNOT_CREATE_OUTPUTFILE
	if ((wrtPtr = fopen(outputFileAddress, "w")) == NULL) {
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_02);
		return listfile_FAILURE;
	} else if ((rdPtr = fopen(fileSystemName, "rb")) == NULL) {
		printf("FileSystem could not be opened by ListFile\n");
		return listfile_FAILURE;
	} else {
		// Seek to buffer area to write file

		fseek(rdPtr, temp->fileDescOffset, SEEK_SET);
		fread(&fDes, sizeof(FileDescriptor), 1, rdPtr);
		size = fDes.size;
	//	     printf("Size of %s is : %ld found at %ld\n", fDes.name, size, temp->fileDescOffset);

		fseek(rdPtr, fDes.startBlock, SEEK_SET);
	//	      printf("Exporting File : %s\n", fDes.name);
	//	    printf("Reached to fileBuffer Location : %ld, size : %ld\n", ftell(rdPtr), size);
		while (size > 0) { // Read file from HD and Write it to VFS
			fread(&ch, sizeof(char), 1, rdPtr);
			fwrite(&ch, sizeof(char), 1, wrtPtr);
			size--; //loop is running file size + 1 
			//  printf("%c",ch);             //times as it is reading EOF, writing 
		} //it to VFS and then checking if EOF is reached

		fclose(rdPtr);
		fclose(wrtPtr);
		printf("listfile_SUCCESS\n");
		return listfile_SUCCESS;
	}

	printf("listfile_FAILURE\n");
	return listfile_FAILURE;
}

// Update the file present on VFS, with one available on HD //

int updateFile(char* filePathInVFS, char* sourceFileAddress) {

	//  printf("Updating File");
	FILE *wrtPtr;
	FileDescriptor fDes;
	long offset = 0, sBlock;
	int size = 0;
	directory* temp;
	buffer buff;
	
	if (mountStatus == unMounted) {
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_05);
		return updatefile_FAILURE;
	}
	
	if(head==NULL){
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_02);
		return updatefile_FAILURE;
	}
	

	if (invalidChar(filePathInVFS, 1) == 1) {
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_02);
		return updatefile_FAILURE;
	}


	char targetName[fileNameLength];
	char sourceName[fileNameLength];

	iNode* inode = (iNode*) malloc(sizeof(iNode));
	inode->dirName = (char*) malloc(fileNameLength); // Reach till the File

	char* targetPath = (char*) malloc(strlen(filePathInVFS) + 5);
	strcpy(targetPath, filePathInVFS);

	char* sourcePath = (char*) malloc(strlen(sourceFileAddress) + 5);
	strcpy(sourcePath, sourceFileAddress);


	if(traceIPath(filePathInVFS, inode)==NULL){
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_05);
		return updatefile_FAILURE;// VFS NOT MOUNTED
	}
	temp = inode->parent;

	offset = temp->fileDescOffset; // fileDescriptor Offset

	strcpy(targetName, getFileDirName(targetPath));
	strcpy(sourceName, getFileDirName(sourcePath));

	//DEST_FILE_PATH_NOT_FOUND
	if (strcmp(targetName, temp->name) != 0 || temp->type!='f') {
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_02);
		return updatefile_FAILURE; // File not exist
	}
	/*
    
	    char *strtok(char *str1, const char *str2);
	    char delims[] = ".";
	    char *targetExtension = strtok(targetName, delims);
	    targetExtension = strtok(NULL, delims);

	    char *sourceExtension = strtok(sourceName, delims);
	    sourceExtension = strtok(NULL, delims);
	 */

	//targetExtension=NULL
	// printf(":%s:%s:",targetExtension, sourceExtension);
	//INCORRECT_FILE_FORMAT
	/*
	    if (strcmp(targetExtension, sourceExtension) != 0){
		printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_03);
		return updatefile_FAILURE;
	    }
    
	 */
	//SOURCE_FILE_PATH_NOT_FOUND
	if ((wrtPtr = fopen(sourceFileAddress, "rb")) == NULL) {
		printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_01);
		return updatefile_FAILURE;
	}
	fclose(wrtPtr);

	//FILE_SYSTEM_NOT_ACCESSIBLE
	if ((wrtPtr = fopen(fileSystemName, "rb+")) == NULL) {
		printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_02);
		return updatefile_FAILURE;
	} else {
		// Seek to Start of File Descriptor array
		fseek(wrtPtr, offset, SEEK_SET);
		fread(&fDes, sizeof(FileDescriptor), 1, wrtPtr);
		sBlock = fDes.startBlock;
		fseek(wrtPtr, sBlock, SEEK_SET);
		fread(&buff, sizeof(buffer), 1, wrtPtr);
		size = updateBuffer((sBlock - bufferOffset) / sizeof(buffer), sourceFileAddress);

		if (size == -2) {
			fseek(wrtPtr, sBlock, SEEK_SET);
			fwrite(&buff, sizeof(buffer), 1, wrtPtr);
			printf("updatefile_FAILURE %s\n", ERR_VFS_UPDATEFILE_04);
			return updatefile_FAILURE;
		} else {
			fDes.size = size;
			// fseek(wrtPtr, -sizeof (FileDescriptor), SEEK_CUR);
			fseek(wrtPtr, offset, SEEK_SET);
			fwrite(&fDes, sizeof(FileDescriptor), 1, wrtPtr);
		}
		fclose(wrtPtr);
		printf("updatefile_SUCCESS\n");
		return updatefile_SUCCESS;
	}
	printf("updatefile_FAILURE\n");
	return updatefile_FAILURE;
}

// Delete a file from VFS //

int removeFile(char* fileAddress) {
	//printf("Removing File");
	long offset = 0;
	directory* temp;
	directory* parentDir;
	iNode* inode = (iNode*) malloc(sizeof(iNode));
	inode->dirName = (char*) malloc(fileNameLength);

	char* path = (char*) malloc(strlen(fileAddress) + 5);
	strcpy(path, fileAddress);

	if (mountStatus == unMounted) {
	printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_02);//VFS NOT MOUNTED
	return removefile_FAILURE;
	} // VFS_NOT_MOUNTED

	if(traceIPath(fileAddress, inode)==NULL){
	printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_01);//head is NULL
	return removefile_FAILURE;
	} // Reach till the File

	temp = inode->parent; // pointer to node to be deleted
	parentDir = temp->parent;
	//  printf("Removing File %s", temp->name);

	//CANNOT_FIND_SPECIFIED_PATH_OR_FILE
	if (strcmp(getFileDirName(path), temp->name) != 0 || temp->type != 'f') {
		printf("removefile_FAILURE %s\n", ERR_VFS_REMOVEFILE_01);
		return removefile_FAILURE; // File do not exists
	}
	
	// Delete the file from nArray
	/*
	    if (strcmp(parentDir->child->name, temp->name) == 0)
		parentDir->child = temp->rightSib;
	    else
		temp->leftSib->rightSib = temp->rightSib;    
	    if (temp->rightSib != NULL)
		temp->rightSib->leftSib = temp->leftSib;
	 */

	// end delete from nAry
	if (deleteNode(temp) == 0) {
		offset = setFileDescriptorFree(temp->fileDescOffset);
		setBlockFree(offset, 0);
		deleteFile(temp->name,temp->fileDescOffset);//delete entry from hashtable
		delete_node(findPath(temp));
		printf("removefile_SUCCESS\n");
		return removefile_SUCCESS;
	}
	printf("removefile_FAILURE\n");
	return removefile_FAILURE;
}


// Move file from one directory to another directory //

int moveFile(char* sourceFileAddress, char* destDirAddress) {

	int status = 0;

	// printf("Moving File");
	
	if (mountStatus == unMounted) {
	printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_06);
	return movefile_FAILURE;
	}

	status = moveNode(sourceFileAddress, destDirAddress, 'f');
	//printf("%d\n",status);
	if (status == 0) {
		printf("movefile_SUCCESS\n");
		return movefile_SUCCESS;
	} else if (status == 1) {//CANNOT_FIND_SPECIFIED_SOURCEFILE
		printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_01);
		return movefile_FAILURE;
	} else if (status == 2) {//CANNOT_FIND_SPECIFIED_DESTINATIONFILE
		printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_02);
		return movefile_FAILURE;
	}
	else if(status==3){//DESTINATION_ALREADY_HAVE_SOURCE_FILE
	printf("movefile_FAILURE\n");
	return movefile_FAILURE;
	}

	else if (status == 4) {//DESTINATION_CANNOT_BE_FILE
		printf("movefile_FAILURE %s \n", ERR_VFS_MOVEFILE_02);
		return movefile_FAILURE;
	}

	else if (status == 5) {//SOURCE_CANNOT_BE_DIR
		printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_01);
		return movefile_FAILURE;
	} else if (status == 6) {//VFS NOT MOUNTED
		printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_01);
		return movefile_FAILURE;
	} else {//If fileSystem Could Not be updated
		printf("movefile_FAILURE\n");
		return movefile_FAILURE;
	}


}

// Copy file from VFS to HD //

int exportFile(char* sourceFileAddress, char* destFileAddress) {
	//  printf("Exporting File");
	FILE *rdPtr;
	FILE *wrtPtr;
	directory *temp;
	char ch = 'p';
	int size = 0;
	char* path;

	if(mountStatus==unMounted){
		printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_07);
		return exportfile_FAILURE;
	}
	
	
	path = (char*) malloc(strlen(sourceFileAddress) + 5);
	strcpy(path, sourceFileAddress);
	FileDescriptor fDes;
	iNode* inode = (iNode*) malloc(sizeof(iNode));
	inode->dirName = (char*) malloc(fileNameLength);

	if(traceIPath(sourceFileAddress, inode)==NULL){//CANNOT_FIND_SOURCEFILE
		printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_01);
		return exportfile_FAILURE;
	}
	temp = inode->parent;
	
	//CANNOT_FIND_SPECIFIED_SOURCEFILE
	if (strcmp(getFileDirName(path), temp->name) != 0) {
		printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_01);
		return exportfile_FAILURE; // File do not exists
	}
	
	//CANNOT_EXPORT_DIR
	if (temp->type == 'd') {
		printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_06);
		return exportfile_FAILURE;
	}
	
	if ((rdPtr = fopen(fileSystemName, "rb")) == NULL) {
		printf("exportfile_FAILURE\n");
		return exportfile_FAILURE;
	}else if ((wrtPtr = fopen(destFileAddress, "wb")) == NULL) {
		printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_02);
		return exportfile_FAILURE;
	} else {
		// Seek to buffer area to write file

		// Apply some checks on temp
		fseek(rdPtr, temp->fileDescOffset, SEEK_SET);
		fread(&fDes, sizeof(FileDescriptor), 1, rdPtr);
		size = fDes.size;
		//      printf("Size of %s is : %ld found at %ld\n", fDes.name, size, temp->fileDescOffset);

		fseek(rdPtr, fDes.startBlock, SEEK_SET);
		//       printf("Exporting File : %s\n", fDes.name);
		//     printf("Reached to fileBuffer Location : %ld, size : %ld\n", ftell(rdPtr), size);
		while (size > 0) { // Read file from HD and Write it to VFS
			fread(&ch, sizeof(char), 1, rdPtr);
			fwrite(&ch, sizeof(char), 1, wrtPtr);
			size--; //loop is running file size + 1 
			//  printf("%c",ch);             //times as it is reading EOF, writing 
		} //it to VFS and then checking if EOF is reached

		fclose(rdPtr);
		fclose(wrtPtr);
		printf("exportfile_SUCCESS\n");
		return exportfile_SUCCESS;
	}
	printf("exportfile_FAILURE\n");
	return exportfile_FAILURE;
}


// Search file in VFS //
int searchFileHash(char* fileName, char* outputFileName){
	
	if(mountStatus==unMounted){
		printf("searchfile_FAILURE %s\n", ERR_VFS_SEARCHFILE_01);
		return searchfile_FAILURE;
	}
	if(head==NULL){
		printf("searchfile_FAILURE\n");
	return searchfile_FAILURE;
	}
	if(searchHashFile(fileName, outputFileName)==0){
	printf("searchfile_SUCCESS\n");
	return searchfile_SUCCESS;
	}else{
	printf("searchfile_FAILURE\n");
		return searchfile_FAILURE;
	}
		
}

/*
int searchFile(char* fileName, char* outputFileName){
//	printf("Searching file\n");
//	printf("filename %s\n",fileName);
	//printf("outputfilename %s \n",outputFileName);

	if(mountStatus==unMounted){
		printf("searchfile_FAILURE %s\n", ERR_VFS_SEARCHFILE_01);
		return searchfile_FAILURE;
	}
	if(head==NULL){
		printf("searchfile_FAILURE\n");
	return searchfile_FAILURE;
	}
	
	char **filepaths;
       FILE *fp;
      int i=0;
      bst *top = gethead();
      if(top==NULL){
       printf("searchfile_FAILURE\n");
		return searchfile_FAILURE;
      }
      
	    filepaths = search_file(fileName,top);
	    
	   if( filepaths==NULL || (fp = fopen(outputFileName,"wb"))==NULL){
		   printf("searchfile_FAILURE\n");
		return searchfile_FAILURE;
	   }else{
		   
	    while(filepaths[i]!=NULL){
		fprintf(fp,"%s",filepaths[i]);
		fprintf(fp,"\n");
		i++;
	    }
	printf("searchfile_SUCCESS\n");
	return searchfile_SUCCESS;
	   }
}

*/

int copyFile(char* sourceFileAddress, char* destDirAddress) {

	//  printf("Copying File");
	FILE* updtPtr;
	directory* temp = head;
	directory* temp2 = head;
	FileDescriptor flDes;
	buffer buff;
	long offset;
	char address[addressLength];
	char *name;
	iNode* inode = (iNode*) malloc(sizeof(iNode));
	inode->dirName = (char*) malloc(fileNameLength);

	if(mountStatus==unMounted){
		printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_07);
		return copyfile_FAILURE;
	}
	
	if(head==NULL){
		printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_01);
	return copyfile_FAILURE;
	}
	
	//FILE_SYSTEM_FULL
	if (noOfFiles == noOfFileDescriptors) {
		printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_06);
		return copyfile_FAILURE;
	}

	char* path = (char*) malloc(strlen(sourceFileAddress) + 5);
	strcpy(path, sourceFileAddress);
	name = getFileDirName(path);

	if(traceIPath(sourceFileAddress, inode)==NULL){
		printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_07);
		return copyfile_FAILURE; // VFS_NOT_MOUNTED
	}
	if (inode->parent != NULL)
		temp = inode->parent;

	//CANNOT_FIND_SPECIFIED_SOURCEFILE
	if (strcmp(name, temp->name) != 0) {
		printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_01);
		return copyfile_FAILURE; // File not exists
	}
	//CANNOT_COPY_DIR_TO_FILE
	if (temp->type == 'd') {
		printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_05);
		return copyfile_FAILURE;
	}

	strcpy(address, destDirAddress);
	if (strcmp(destDirAddress, "/") != 0)
		strcat(address, "/");
	strcat(address, temp->name);
	//   printf("Address of destination file will be : %s\n", address);

	// Code to update naryTree
	temp2 = creatNode(destDirAddress, temp->name, temp->type, 1);
	if (temp2 != NULL) {
		//      printf("Tree Updated\n");

		if (strcmp(temp2->name, temp->name) != 0) {
			printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_02);
			return copyfile_FAILURE;
		}

		if ((updtPtr = fopen(fileSystemName, "rb+")) == NULL) {
			printf("copyfile_FAILURE\n");
		} else {
			//         printf("Reading file descriptor from location %ld\n", temp->fileDescOffset);
			fseek(updtPtr, temp->fileDescOffset, SEEK_SET);
			fread(&flDes, sizeof(FileDescriptor), 1, updtPtr);

			fseek(updtPtr, flDes.startBlock, SEEK_SET);
			fread(&buff, sizeof(buffer), 1, updtPtr);

			offset = updateFreeBlockList();

			fseek(updtPtr, (bufferOffset + offset * sizeof(buffer)), SEEK_SET);
			offset = ftell(updtPtr);
			//         printf("File %s of size %ld will be written at : %ld\n", flDes.name, flDes.size, offset);
			fwrite(&buff, sizeof(buffer), 1, updtPtr);

			offset = updateFileDescriptor(address, temp->name, 'f', flDes.size, offset);

			//          printf("File Descriptor Updated\n");

			temp2->fileDescOffset = offset;
			//         printf("Offset : %ld\n", offset);
			insertFile(temp2->name, offset);
			add_elt(findPath(temp2));
			fclose(updtPtr);
			printf("copyfile_SUCCESS\n");
			return copyfile_SUCCESS;
		}
	} else {
		//      printf("File with same name already exists in directory");
		printf("copyfile_FAILURE\n");
		return copyfile_FAILURE;
	}
	printf("copyfile_FAILURE\n");
	return copyfile_FAILURE;
}
